/**
 * 医疗服务质询初始化模块，提供医疗服务编辑页面初始化组件功能
 * 
 * @Date: 2016-1-25
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */

define(["jquery","sysmanMain","valueUtil","uploadfiles","deptUserSelect","msgServ","formServ"],
		function($,sysmanMain,valueUtil,uploadfiles,deptUserSelect,msgServ,formServ) {
			
		'use strict';
		var edit_window_id = 'ethic-dailyEvaluation-win'
		var rootid = 'panel-ethic-dailyEvaluation'
		var public_method ={			
			beforeSubmit:function(){
				var _form = $('#'+edit_window_id +' form.main-form')
				
				var _createat = $('#'+edit_window_id +' #createat')
				if(_createat.length ==1){
					var _tmpValue = $(_createat).datebox('getValue')
					if(_tmpValue){					
						$(_createat).val(_tmpValue)
					}
				}
				var valid =$(_form).form('enableValidation').form('validate');
				if(!valid){
					
					 return valid
				}
				if(window.console)
					console.info('beforeSubmit event fired ...')
				var _moduleConfig = sysmanMain.getCurrentModuConfig();
				private_method.getIdValue()
				return {bizcode:_moduleConfig.bizcode}
			},
			afterSubmit:function(){
				var idvalue =private_method.getIdValue()
				if(!idvalue || $.trim(idvalue).length<=0){
					$('#'+edit_window_id).window('close')
					$('#'+edit_window_id).empty()
				}
				$('#'+rootid + ' table.dg-list').datagrid('reload')
				msgServ.Msgslide('操作成功');
			},
			rowClick:function(index,data){
				
				if(window.console)
					console.info('inpatient rowClick event fired ...')
				var _config = sysmanMain.getModuConf('inpatientItemFee');
				if(_config ==null){
					_config = {}					
				}	
				
				_config.initQueryParams = {field:'patient_id',fieldVal:data.patient_id}
				sysmanMain.setModuConf('inpatientItemFee',_config)
				sysmanMain.medicalInpatientItemFeeSelect()
			},
			/**
			 * 初始化新增页面
			 * @param _options.record 列表选中行
			 * @param _options.form 页面form对象
			 * @param _options.rootId 页面根节点
			 */
			initElement:function(_options){
				var _rootpath = '/'+sysmanMain.getRootPath()
				
				var deptQueryUrl='/department/query/getTreeList.action'
				//判断form对象是否存在
				if(_options && _options.rootId){
					var _table = $('#' + _options.rootId + ' table.dg-list')
					if(_table.length==1){
						$(_table).datagrid({
							onClickCell:private_method.clickCell
						})
					}
				}
			},
			initEdit:function(_options){
				private_method.initEdit(_options)
			}			
		}
		
		var private_method ={
			getIdValue:function(){
				var idVal
				var _id = $('#'+edit_window_id + ' #id')
				if(_id.length==1){
					idVal = $(_id).val()
					if(idVal){
						if(idVal.indexOf('TMP')>=0){
							$(_id).val('')
						}
					}
					
				}
				return idVal
			},
			
			getTmpidValue:function(){
				var idVal
				var _id = $('#'+edit_window_id + ' #tmpid')
				if(_id.length==1){
					idVal = $(_id).val()				
				}
				return idVal
			},
			/**
			 * 
			 */
			initEdit:function(_options){
				
				
				if(window.console){
					console.log(' initEdit loaded ...')
					
				}
				
				//记录人员
				var _el = $('#'+edit_window_id+' #record_name')
				if(_el &&_el.length>0){
					var _rn = $(_el).val();
					if(!_rn || $.trim(_rn).length==0){
						var usr = sysmanMain.getCurrentUser()
						if(usr && usr.name){
							$(_el).val(usr.name)
						}
					}
				}
				
				//医生姓名
				var _el = $('#'+edit_window_id+' #eval_name')
				if(_el &&_el.length>0){
					
					$(_el).textbox({
						onClickButton:function(data){
							var _userParam = {
								userids:edit_window_id +' #eval_id',
								usernames:edit_window_id+' #eval_name',
								deptid:edit_window_id +' #org_id',
								deptname:edit_window_id+' #org_name',
								selectType:'job',
								subsystem:'ethic',
								required : true
							}
							deptUserSelect.openPage(_userParam)							
						}
					})
				}
				//加扣分
				var _mainSelect = $(_options.form).find('#addormin')
				var selectedRecord = _options.record
				var _rootpath = '/'+sysmanMain.getRootPath()
				$(_mainSelect).combobox({
					
					textField:'name',
					valueField:'id',
					url:_rootpath + '/dictionary/getTree.action?id=' + 126,
					required : true,
					onSelect:function(record){
						$(_options.form).find('#addorminus_type').val(record.name)
						private_method.changeSubItem({form:_options.form,record:record})
						$(_mainSelect).val(record.id);
					},
					onLoadSuccess:function(){
						if(selectedRecord && selectedRecord.addorminus_type){
							var _data = $(_mainSelect).combobox('getData')
							$.each(_data,function(i,r){
								if(r.name ===selectedRecord.addorminus_type){
									$(_mainSelect).combobox('select',r.id)
								}
							})							
						}
					}
				})
				
				
				//加扣分项
				
				
				if(selectedRecord && selectedRecord.addorminus_id && selectedRecord.item_name){
					
					var _data ={
						id      :selectedRecord.addorminus_id,
						text    :selectedRecord.item_name,
						selected:true,
						required:true,
						onSelect:function(record){
							$(_itemSelect).val(record.id);
						}
					}
					var _itemSelect = $(_options.form).find('#addorminus_id')
				
					$(_itemSelect).combobox({data:_data})					
				}
				
				
				
				//记录时间
				var _createat = $('#'+edit_window_id +' #createat')
				if(_createat.length ==1){
					var _tmpValue = $(_createat).datebox('getValue')
					if(!_tmpValue)
					valueUtil.setMonthDate({target:_createat})
					else{
						$(_createat).val(_tmpValue)
					}
				}
				
				var _id = $('#'+edit_window_id +' #id')
				if(_id.length ==1){
					
					
					var _tmpValue = $(_id).val()
					//id为空
					if(!_tmpValue || _tmpValue==''){
						var _date = new Date();
						_date = _date.getFullYear()+''+(_date.getMonth()+1)+''+_date.getDate()+'' +_date.getMilliseconds();
						var _tmpid = $('#'+edit_window_id +' #tmpid')
						$(_tmpid).val('TMP'+_date)
						$(_id).val('TMP'+_date)
					}
				}
				
			},
			initElement:function(_options){
				
				var _mainSelect = $(_options.form).find('#inquiry_item_code')
				$(_mainSelect).combobox({
					width:el_width,
					required:true,
					readonly:false,
					textField:'name',
					valueField:'id',
					url:_rootpath + '/dictionary/getTree.action?id=' + 4,
					onSelect:function(record){
						var _item = $(_options.form).find('#inquiry_item')
						$(_item).val(record.text)
						$(_mainSelect).val(record.id);
						private_method.changeSubItem({form:_options.form,record:record})
					},
					onLoadSuccess:function(){
						if(selectedRecord && selectedRecord.inquiry_item_code){
							$(_mainSelect).combobox('select',selectedRecord.inquiry_item_code)
							private_method.changeSubItem({form:_options.form,record:{id:selectedRecord.inquiry_item_code}})
						}
					}
				})
			},
			clickCell:function(index,field,value){
				if(window.console)
					console.info(' clickCell fired ...')
				
				if(field =='arc_num'){
					var $bizgrid= $('#'+rootid + ' table.dg-list')
					var record = $($bizgrid).datagrid('getRows')[index];				
					var _moduleConfig = sysmanMain.getCurrentModuConfig();
					var rootPath = '/' +sysmanMain.getRootPath()
					var uploadParam = {
								data:{
									bizno:record.id,
									bizcode:_moduleConfig.moduleCode
								},
								rootPath:rootPath							
							}
					uploadfiles.imgShow(uploadParam)
				}
			},
			/**
			 * 更新质询子类信息
			 * @param _options.form	
			 * @param _options.record
			 * @param _options.target
			 */
			changeSubItem:function(_options){
				var _subSelect = $(_options.form).find('#addorminus_id')
				var _rootpath = '/'+sysmanMain.getRootPath()
				
				var param 
				if(_options.record.name){
					
					switch(_options.record.name){
						case '加分':
							param = 1;
							break;
						case '扣分':
							param = 2;
							break;
						case '一票否决':
							param = 3;
							break;
						defalut:
							param = 1;
							break;
					}
					param = '&fieldVal=' +param
				}
				$(_subSelect).combobox({
					textField:'item_name',
					valueField:'id',
					readonly:false,
					url:_rootpath + '/addorminus/query/getListByField.action?field=item_type' + param,
					onSelect:function(record){
						if(record && record.score)
						$('#'+edit_window_id + ' #score').textbox('setValue',record.score)
						$(_subSelect).val(record.id)
					}
				})				
			}
			
		}
			
		
		return public_method;
			
})